#include <iostream>
#include <string>

#include "Log.hpp"
#include "Listener.hpp"
#include "Connetion.hpp"
#include "EpollServer.hpp"

using namespace LogModule;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " Port" << std::endl;
        return 1;
    }

    ENABLE_CONSOLE_LOG();
    uint16_t local_port = std::stoi(argv[1]);

    Listener listen(local_port);
    LOG(LogLevel::DEBUG) << "Listener create success..."<< local_port;

    auto conn = std::make_shared<Connection>(listen.Sockfd());
    LOG(LogLevel::DEBUG) << "conn create success...";

    conn->InitCB(
        [&listen]()
        {
            listen.Accepter();
        },
        nullptr,
        nullptr);
    conn->SetEvents(EPOLLIN | EPOLLOUT);
    LOG(LogLevel::DEBUG) << "conn SetEvents create success...";

    EpollServer epoll_svr;
    LOG(LogLevel::DEBUG) << "EpollServer create success...";

    epoll_svr.InsertConnection(conn);
    LOG(LogLevel::DEBUG) << "InsertConnection create success...";

    epoll_svr.Loop();
    

    return 0;
}